{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 老师公布可行答案"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import tensorflow as tf\n",
    "from tensorflow.examples.tutorials.mnist import input_data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Extracting MNIST_data\\train-images-idx3-ubyte.gz\n",
      "Extracting MNIST_data\\train-labels-idx1-ubyte.gz\n",
      "Extracting MNIST_data\\t10k-images-idx3-ubyte.gz\n",
      "Extracting MNIST_data\\t10k-labels-idx1-ubyte.gz\n",
      "WARNING:tensorflow:From <ipython-input-2-3c1fd293511c>:31: softmax_cross_entropy_with_logits (from tensorflow.python.ops.nn_ops) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "\n",
      "Future major versions of TensorFlow will allow gradients to flow\n",
      "into the labels input on backprop by default.\n",
      "\n",
      "See tf.nn.softmax_cross_entropy_with_logits_v2.\n",
      "\n",
      "Iter 0, Testing Accuracy= 0.9501, Learning Rate= 0.001\n",
      "Iter 1, Testing Accuracy= 0.9641, Learning Rate= 0.00095\n",
      "Iter 2, Testing Accuracy= 0.9694, Learning Rate= 0.0009025\n",
      "Iter 3, Testing Accuracy= 0.9724, Learning Rate= 0.000857375\n",
      "Iter 4, Testing Accuracy= 0.9717, Learning Rate= 0.000814506\n",
      "Iter 5, Testing Accuracy= 0.9757, Learning Rate= 0.000773781\n",
      "Iter 6, Testing Accuracy= 0.9751, Learning Rate= 0.000735092\n",
      "Iter 7, Testing Accuracy= 0.9754, Learning Rate= 0.000698337\n",
      "Iter 8, Testing Accuracy= 0.9775, Learning Rate= 0.00066342\n",
      "Iter 9, Testing Accuracy= 0.9778, Learning Rate= 0.000630249\n",
      "Iter 10, Testing Accuracy= 0.9787, Learning Rate= 0.000598737\n",
      "Iter 11, Testing Accuracy= 0.9806, Learning Rate= 0.0005688\n",
      "Iter 12, Testing Accuracy= 0.9802, Learning Rate= 0.00054036\n",
      "Iter 13, Testing Accuracy= 0.9802, Learning Rate= 0.000513342\n",
      "Iter 14, Testing Accuracy= 0.9798, Learning Rate= 0.000487675\n",
      "Iter 15, Testing Accuracy= 0.9794, Learning Rate= 0.000463291\n",
      "Iter 16, Testing Accuracy= 0.9791, Learning Rate= 0.000440127\n",
      "Iter 17, Testing Accuracy= 0.9801, Learning Rate= 0.00041812\n",
      "Iter 18, Testing Accuracy= 0.9801, Learning Rate= 0.000397214\n",
      "Iter 19, Testing Accuracy= 0.9807, Learning Rate= 0.000377354\n",
      "Iter 20, Testing Accuracy= 0.981, Learning Rate= 0.000358486\n",
      "Iter 21, Testing Accuracy= 0.9804, Learning Rate= 0.000340562\n",
      "Iter 22, Testing Accuracy= 0.9808, Learning Rate= 0.000323534\n",
      "Iter 23, Testing Accuracy= 0.9802, Learning Rate= 0.000307357\n",
      "Iter 24, Testing Accuracy= 0.978, Learning Rate= 0.000291989\n",
      "Iter 25, Testing Accuracy= 0.9818, Learning Rate= 0.00027739\n",
      "Iter 26, Testing Accuracy= 0.9806, Learning Rate= 0.00026352\n",
      "Iter 27, Testing Accuracy= 0.9819, Learning Rate= 0.000250344\n",
      "Iter 28, Testing Accuracy= 0.9822, Learning Rate= 0.000237827\n",
      "Iter 29, Testing Accuracy= 0.982, Learning Rate= 0.000225936\n",
      "Iter 30, Testing Accuracy= 0.982, Learning Rate= 0.000214639\n",
      "Iter 31, Testing Accuracy= 0.9825, Learning Rate= 0.000203907\n",
      "Iter 32, Testing Accuracy= 0.9813, Learning Rate= 0.000193711\n",
      "Iter 33, Testing Accuracy= 0.9816, Learning Rate= 0.000184026\n",
      "Iter 34, Testing Accuracy= 0.9822, Learning Rate= 0.000174825\n",
      "Iter 35, Testing Accuracy= 0.9817, Learning Rate= 0.000166083\n",
      "Iter 36, Testing Accuracy= 0.9816, Learning Rate= 0.000157779\n",
      "Iter 37, Testing Accuracy= 0.9816, Learning Rate= 0.00014989\n",
      "Iter 38, Testing Accuracy= 0.9818, Learning Rate= 0.000142396\n",
      "Iter 39, Testing Accuracy= 0.9815, Learning Rate= 0.000135276\n",
      "Iter 40, Testing Accuracy= 0.9821, Learning Rate= 0.000128512\n",
      "Iter 41, Testing Accuracy= 0.9811, Learning Rate= 0.000122087\n",
      "Iter 42, Testing Accuracy= 0.9822, Learning Rate= 0.000115982\n",
      "Iter 43, Testing Accuracy= 0.9816, Learning Rate= 0.000110183\n",
      "Iter 44, Testing Accuracy= 0.9812, Learning Rate= 0.000104674\n",
      "Iter 45, Testing Accuracy= 0.9829, Learning Rate= 9.94403e-05\n",
      "Iter 46, Testing Accuracy= 0.9821, Learning Rate= 9.44682e-05\n",
      "Iter 47, Testing Accuracy= 0.9813, Learning Rate= 8.97448e-05\n",
      "Iter 48, Testing Accuracy= 0.9818, Learning Rate= 8.52576e-05\n",
      "Iter 49, Testing Accuracy= 0.9826, Learning Rate= 8.09947e-05\n",
      "Iter 50, Testing Accuracy= 0.9821, Learning Rate= 7.6945e-05\n"
     ]
    }
   ],
   "source": [
    "#载入数据集\n",
    "mnist = input_data.read_data_sets(\"MNIST_data\",one_hot=True)\n",
    "\n",
    "#每个批次的大小\n",
    "batch_size = 100\n",
    "#计算一共有多少个批次\n",
    "n_batch = mnist.train.num_examples // batch_size\n",
    "\n",
    "#定义两个placeholder\n",
    "x = tf.placeholder(tf.float32,[None,784])\n",
    "y = tf.placeholder(tf.float32,[None,10])\n",
    "keep_prob=tf.placeholder(tf.float32)\n",
    "lr = tf.Variable(0.001, dtype=tf.float32)\n",
    "\n",
    "#创建一个简单的神经网络\n",
    "W1 = tf.Variable(tf.truncated_normal([784,500],stddev=0.1))\n",
    "b1 = tf.Variable(tf.zeros([500])+0.1)\n",
    "L1 = tf.nn.tanh(tf.matmul(x,W1)+b1)\n",
    "L1_drop = tf.nn.dropout(L1,keep_prob) \n",
    "\n",
    "W2 = tf.Variable(tf.truncated_normal([500,300],stddev=0.1))\n",
    "b2 = tf.Variable(tf.zeros([300])+0.1)\n",
    "L2 = tf.nn.tanh(tf.matmul(L1_drop,W2)+b2)\n",
    "L2_drop = tf.nn.dropout(L2,keep_prob) \n",
    "\n",
    "W3 = tf.Variable(tf.truncated_normal([300,10],stddev=0.1))\n",
    "b3 = tf.Variable(tf.zeros([10])+0.1)\n",
    "prediction = tf.nn.softmax(tf.matmul(L2_drop,W3)+b3)\n",
    "\n",
    "#交叉熵代价函数\n",
    "loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y,logits=prediction))\n",
    "#训练\n",
    "train_step = tf.train.AdamOptimizer(lr).minimize(loss)\n",
    "\n",
    "#初始化变量\n",
    "init = tf.global_variables_initializer()\n",
    "\n",
    "#结果存放在一个布尔型列表中\n",
    "correct_prediction = tf.equal(tf.argmax(y,1),tf.argmax(prediction,1))#argmax返回一维张量中最大的值所在的位置\n",
    "#求准确率\n",
    "accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))\n",
    "\n",
    "with tf.Session() as sess:\n",
    "    sess.run(init)\n",
    "    for epoch in range(51):\n",
    "        sess.run(tf.assign(lr, 0.001 * (0.95 ** epoch)))\n",
    "        for batch in range(n_batch):\n",
    "            batch_xs,batch_ys =  mnist.train.next_batch(batch_size)\n",
    "            sess.run(train_step,feed_dict={x:batch_xs,y:batch_ys,keep_prob:1.0})\n",
    "        \n",
    "        learning_rate = sess.run(lr)\n",
    "        acc = sess.run(accuracy,feed_dict={x:mnist.test.images,y:mnist.test.labels,keep_prob:1.0})\n",
    "        print (\"Iter \" + str(epoch) + \", Testing Accuracy= \" + str(acc) + \", Learning Rate= \" + str(learning_rate))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
